package com.ysjl.intentservice;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

/**
 * 学习使用 IntentService 和 LocalBroadcastManager
 *
 * Android中BroadcastReceiver主要用途有

 发送通知，更新UI或者数据，应用程序间相互通信，监听系统状态（比如开机，网络等）

 在android-support-v4.jar中引入了LocalBroadcastManager，
 称为局部通知管理器，这种通知的好处是安全性高，效率也高，适合局部通信，可以用来代替Handler更新UI

 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private LocalBroadcastManager localBroadcastManager;
    private BroadcastReceiver broadcastReceiver;
    private static final String START = "dddd";
    private static final String EDT = "end";
    private static final String UP = "update";

    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        localBroadcastManager = LocalBroadcastManager.getInstance(this);

        findViewById(R.id.bt_start).setOnClickListener(this);
        findViewById(R.id.bt_end).setOnClickListener(this);

        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                Log.i("mylog","onReceive");

                if(intent.getAction().equals(START)){
                    Log.i("mylog",intent.getStringExtra("data")+"dddd");
                }else if(intent.getAction().equals(EDT)){
                    Log.i("mylog",intent.getStringExtra("data"));
                }
            }
        };


        IntentFilter filter = new IntentFilter();
        filter.addAction(START);
        filter.addAction(EDT);

        localBroadcastManager.registerReceiver(broadcastReceiver,filter);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_start:
                Intent intent = new Intent(MainActivity.this,MyIntentService.class);
                startService(intent);
                break;
            case R.id.bt_end:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (true){
                            Intent intent1 = new Intent();
                            intent1.putExtra("data",(i++)+"");
                            intent1.setAction("end");
                            localBroadcastManager.sendBroadcast(intent1);

                            try {
                                Thread.sleep(3000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                }).start();
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(broadcastReceiver);
    }
}
